For line 3 please take a look at a way at calling Import-csv for a list of device IDs so we don’t deploy to all, Script below is looking for a device and matching it against the owner info then writing the ExAttrib from the user account to the device ExAttrib in AAD.

Connect-MgGraph -Scopes “Directory.AccessAsUser.All”
Select-MgProfile Beta
##[array]$Devices = Get-MgDevice -All ### Need to change to list of Device ID from CSV for testing

ForEach ($Device in $Devices) {
If ($Device.PhysicalIds.count -gt 0) {
Foreach ($X in $Device.PhysicalIds) { If ($X.SubString(0,10) -eq “[USER-GID]”) { $UserGuid = $X } }
$UserId = $UserGuid.substring(11,36)
If ($UserId) { #We found a user identifier - try to resolve it against Azure AD
[array]$User = Get-MgUser -UserId $UserId -ErrorAction SilentlyContinue }
If ($User) { # Found a user in Azure AD
Write-Host (“Device {0} owned by {1}” -f $Device.DisplayName, $User.DisplayName)
$Attributes = @{
“extensionAttributes” = @{
“extensionAttribute13” = $User.extensionAttribute13

} | ConvertTo-Json
Update-MgDevice -DeviceId $Device.Id -BodyParameter $Attributes
}
Else { Write-Host (“Device {0} owned by unknown user {1}” -f $Device.DisplayName, $UserId ) }
} # End If Device PhysicalsId
} #End Foreach

7 Spice ups

When inserting code, please use the “Insert Code” button </>. It makes it a lot easier to read.

Finding the device is dependent on what’s in your CSV and how it’s formatted. Here’s a generic idea

$Devices = Import-Csv -Path "C:\Path\File.csv"

$Devices | ForEach-Object {
    $Device = Get-MGDevice -DeviceId $_.DEVICEIDCOLUMNNAME -ErrorAction SilentlyContinue
    # OR
    $Device = Get-MGDevice -Filter "extensionAttributes/extensionAttribute1 eq 'BYOD-Device'"

    if ($null -ne $Device) {
        # DO STUFF
    }
}

I’d recommend using the Filter option if possible. The documentation has a number of samples on finding devices.

1 Spice up

Write-Host Kills Puppies

2 Spice ups

As of PowerShell 5, the puppies are safe, whew!

Write-Host became (in essence) a wrapper for
Write-Information -InformationAction Continue in PSv5, presumably because:

    it enables suppressing or redirecting Write-Host messages, which was not previously possible (in PowerShell 4 or below, Write-Host bypassed PowerShell's streams and output directly to the host),

    while preserving backward compatibility in that the messages are output by default - unlike with Write-Information, whose default behavior is to be silent (because it respects preference variable $InformationPreference, whose default value is SilentlyContinue).

While Write-Host is therefore now (PSv5+) a bit of a misnomer — it doesn't necessarily write to the host anymore — it still has one distinct advantage over Write-Information (as you state): it can produce colored output with -ForegroundColor and -BackgroundColor

https://stackoverflow.com/questions/38523369/write-host-vs-write-information-in-powershell-5

I will also add, if you’re bent on writing colored output to the screen, this is the simplest way

Hi thank you for the help and can you please modify the same script I have shared it will help me a lot

I posted a sample skeleton that you can try. There are a lot of unknowns with your code and your question. We’re not mind readers or a script on demand service. Please try what’s above and let us know if you have further questions or need additional help.